package com.wqr.it.ecwdr.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.wqr.it.ecwdr.dto.ProductDto;
import com.wqr.it.ecwdr.dto.StockDto;
import com.wqr.it.ecwdr.service.StockService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/stock")
public class StockController {
	@Autowired
	private StockService stockService;

	@RequestMapping(value="/list/product", method = RequestMethod.GET)
	public List<ProductDto> findProductAll() {
		System.out.println("StockController.findProductAll");
		List<ProductDto> list = stockService.findProductAll();
		System.out.println("StockController.findProductAll, size: " + list.size());
		return list;
	}

	@ApiOperation(value = "更新库存信息", notes = "更新库存信息")
	@ApiImplicitParam(name = "StockDto", value = "库存信息的json表示方式 ", required = true, dataType = "String")
	@RequestMapping(value = "/post", method = RequestMethod.POST)
	public void post(@RequestParam("StockDto") String stockDtoStr) {
		if (!StringUtils.isEmpty(stockDtoStr)) {
			StockDto stockDto = JSONObject.parseObject(stockDtoStr, StockDto.class);
			stockService.save(stockDto);
		}
	}

	@ApiOperation(value = "刷新库存Token", notes = "刷新库存Token")
	@RequestMapping(value="/refreshToken", method = RequestMethod.GET)
	public int refreshStockToken() {
		return stockService.refreshToken();
	}
}
